package cabbage.database.operations;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cabbage.database.idao.ILocationDAO;
import cabbage.database.utilities.DataBaseConnection;

public class LocationDAO implements ILocationDAO {
	
	private DataBaseConnection db;
	
	public LocationDAO() {
		
	}
	
	@Override
	public boolean checkLevelNumberExists(int number) throws SQLException {
		db = new DataBaseConnection();
		Connection conn = db.getConnection();
		String sqlString = "SELECT z FROM location WHERE z=?";
		PreparedStatement ps = conn.prepareStatement(sqlString);
		ps.setInt(1, number);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			conn.close();
			
			return true;
		}
		conn.close();
		
		return false;
	}

	/**
	 * Returns true if a given location_id exists in the location table.
	 * @param location_id - the int id to search for.
	 * @return true or false - boolean.
	 * @throws SQLException
	 */
	@Override
	public boolean checkLocationidExists(int id) throws SQLException {
		db = new DataBaseConnection();
		Connection conn = db.getConnection();
		String sqlString = "SELECT * FROM location WHERE location_id=?";
		PreparedStatement ps = conn.prepareStatement(sqlString);
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			conn.close();
			
			return true;
		}
		conn.close();
		
		return false;
	}

}
